local blaze = require "blaze"
local game = require "club.threecard.texas"
local consts = game.util.consts
local log = blaze.logging.get("Seat")

local Time = CS.UnityEngine.Time

local operator = game.require "ui.ingame.operator"

local M = game.ui.element()

function M:ctor(no)
    self.player = nil
    self.no = no
    self.res = "club.threecard.texas/ui/ingame.b:seat"
    self.avatar_icon = require("ui.Common.AvatarIcon").new()

    local container = game.pokerlib.core.poker_container
    local poker_container_config = game.require "poker_controller.poker_container_config"
    if self.no == 1 then
        self.poker_container = container.new(poker_container_config.self_config)
    else
        self.poker_container = container.new(poker_container_config.ontable_config)
    end
end

function M:on_injected(bind)
    self.poker_container:set_gameobject(self.poker_container_obj, true)
    bind("gameobject.element", self.head_obj, self.avatar_icon)
end

function M:on_load()
    self:super("on_load")
    self.avatar_icon:load()
end

function M:set_player(player)
    self.player = player
    if player then
        self.avatar_icon:show()
        self.avatar_icon:set_player(player)
    else
        self.avatar_icon:hide()
    end
    self:lazy("on_set_player")
end

function M:on_set_player()
    self.poker_container:clear()

    if self.player == nil then
        self.root.gameObject:SetActive(false)
        self.empty_root.gameObject:SetActive(true)
        self.bet_root.gameObject:SetActive(false)
        return
    end

    self.root.gameObject:SetActive(true)
    self.empty_root.gameObject:SetActive(false)

    self.name_txt.text = self.player.name
    self.countdown_root.gameObject:SetActive(false)
    self:update_chip(0, self.player.chip)
end

function M:update_chip(old, current)
    game.utility.digit(self.chip_txt, old, current, game.i18n.styles.chip2)
end

function M:on_turn()
    local state = game.store.ingame.state
    local left_time = state.left_time
    if left_time <= 0 then
        return
    end

    self:clear_co()

    self.countdown_root.gameObject:SetActive(true)
    self.co = game.coroutine.ingame_start(function()
        while left_time > 0 do
            left_time = left_time - Time.deltaTime
            self.countdown_txt.text = math.ceil(left_time)
            game.coroutine.step()
        end
    end)
end

function M:on_wait()
    self:clear_co()
    self.countdown_root.gameObject:SetActive(false)
end

function M:on_flop(value)
    log.debug(game.util.consts.flop.name)
    self.ope_name.text = game.util.consts.flop.name
    for _, poker in ipairs(self.poker_container.hand) do
        poker:gray()
    end
end

function M:on_call(value)
    log.debug(game.util.consts.call.name)
    self.ope_name.text = game.util.consts.call.name
    self:bet(value)
end

function M:on_check(value)
    log.debug(game.util.consts.check.name)
    self.ope_name.text = game.util.consts.check.name
end

function M:on_raise_half_pot(value)
    log.debug(game.util.consts.raise_half_pot.name)
    self.ope_name.text = game.util.consts.raise_half_pot.name
    self:bet(value)
end

function M:on_raise_pot(value)
    log.debug(game.util.consts.raise_pot.name)
    self.ope_name.text = game.util.consts.raise_pot.name
    self:bet(value)
end

function M:on_allin(value)
    log.debug(game.util.consts.allin.name)
    self.ope_name.text = game.util.consts.allin.name
    self:bet(value)
end

function M:on_phase_change()
    self.ope_name.text = ""
    self.bet_root.gameObject:SetActive(false)
end

function M:get_tag_1()
    return self.tag_root1
end

function M:get_tag_2()
    return self.tag_root2
end

function M:bet(value)
    self.bet_root.gameObject:SetActive(true)
    self.bet_value.text = value
end

function M:highlight_card(card_ids)
    local map = {}
    for _, id in ipairs(card_ids) do
        map[id] = true
    end

    for _, poker in ipairs(self.poker_container.hand) do
        if map[poker.data.server_value] then
            poker:highlight()
        else
            poker:gray()
        end
    end

    for _, poker in ipairs(self.poker_container.ming) do
        if map[poker.data.server_value] then
            poker:highlight()
        else
            poker:gray()
        end
    end
end

function M:clear_co()
    if self.co then
        game.coroutine.stop(self.co)
        self.co = nil
    end
end

function M:on_release()
    if not self.loaded then
        return
    end

    self:clear_co()
    self.poker_container_obj.transform:SetParent(self.transform, false)
end

function M:clear_scene()
    self.poker_container:clear()
    self:on_wait()
    self:on_phase_change()
end

return M